﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Text;

namespace eProcurement
{
    public class EmailManager
    {
        public void SendEmail( List<string> to, List<string> cc, string subject, string messagebody )
        {
            if ( ConfigurationManager.AppSettings[ "MailServerEnabled" ] == "TRUE" )
            {
                try
                {
                    MailMessage mailmessage = new MailMessage();
                    SmtpClient client = new SmtpClient();
                    client.Credentials = new System.Net.NetworkCredential( ConfigurationManager.AppSettings[ "MailServerUser" ],
                        ConfigurationManager.AppSettings[ "MailServerPwd" ] );
                    client.Host = ConfigurationManager.AppSettings[ "MailServer" ];
                    client.Port = Convert.ToInt16( ConfigurationManager.AppSettings[ "MailServerPort" ] );
                    client.EnableSsl = true;

                    mailmessage.From = new MailAddress( "eprocurement@agilelight.net" );
                    mailmessage.Body = messagebody;
                    mailmessage.Body = mailmessage.Body + "<br/><br/><div style='font-family:Calibri;font-size:12px'><strong>Note:</strong> This is an Auto Generated e-Mail, Please do not reply to this e-mail.</div>";
                    mailmessage.Subject = subject;
                    mailmessage.IsBodyHtml = true;

                    foreach ( string email in to )
                    {
                        mailmessage.To.Add( email );
                    }

                    foreach ( string email in cc )
                    {
                        mailmessage.CC.Add( email );
                    }

                    //TODO: UNCOMMENT BEFORE PUBLISH**********
                    client.Send( mailmessage );
                }
                catch ( Exception ex )
                {
                    string error = ex.ToString();
                }
            }
        }
    }
}
